home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / perl / examples / perlotine < prev    next >
Encoding:
Text File  |  2000-12-20  |  7.4 KB  |  231 lines

  1. #!/usr/app/bin/perl
  2.  
  3. eval 'exec /usr/app/bin/perl  -S $0 ${1+"$@"}'
  4.     if 0; # not running under some shell
  5.  
  6. # <sjburges@gimp.org>
  7. # This is tigert's request.  I suppose it'll be useful to those that do
  8. # this sort of thing.  Personally I'll probably only run it to test and
  9. # put up a demo image.
  10.  
  11. # Since updated a couple times by others, and intgrated by me:
  12. # Bruce Miller                              (fixed to accomdate 1.1.x changes)
  13. # Brendon and Wendy Humphrey <brendy@swipnet.se> (progress bar, nice comments)
  14. #
  15. # Tuomas Kuosmanen <tigert@gimp.org>
  16. # Fixed some things to make this work with cvs gimp. Fixed calls to
  17. # gimp_get_guide_orientation() (guide types that changed from
  18. # GUIDE_VERTICAL to VERTICAL and horizontal, respectively.  Should
  19. # work now). Also convert to indexed parameters changed, fixed them too.
  20. #
  21. # <sjburges@gimp.org> 
  22. # Added changes to make progess bar work for images with only horizontal
  23. # or vertical guides.  This was fixed at one time, I forget who did that.
  24. # Somehow it got broke again ;(
  25. #
  26. # Added File::Path; for making the directory if it doesn't already exist.
  27. #
  28. # If you have more additions, etc please don't hesitate to send them in!
  29.  
  30. use File::Path;
  31.  
  32. use Gimp;
  33. use Gimp::Fu;
  34. use Gimp::Util;
  35.  
  36. # Uncomment if you want to see everything that's going on.
  37. # Gimp::set_trace(TRACE_ALL);
  38.  
  39. #
  40. # Generates an ordered list of all existing vertical guides.
  41. #
  42.  
  43. sub get_vguides {  
  44.     my ($img)=@_;
  45.     $i=0;
  46.     my @vguides;
  47.     while ($i=$img->find_next_guide($i)) {
  48.         if ($img->get_guide_orientation($i) == &Gimp::VERTICAL){
  49.             $keyval = sprintf("%4d", $img->get_guide_position($i));
  50.             $vkeys{$keyval} = $i;
  51.             }
  52.         }
  53.     foreach $key(sort (keys %vkeys)) {
  54.         # print "Unshifting ",  $key, "\n";
  55.         push @vguides, $vkeys{$key};
  56.         }
  57.     return @vguides;
  58.     }
  59.  
  60. #
  61. # Generates an ordered list of all existing horizontal guides.
  62. #
  63.  
  64. sub get_hguides { 
  65.     my ($img)=@_;
  66.     $i=0;
  67.     my @hguides;
  68.     while ($i=$img->find_next_guide($i)) {
  69.         if ($img->get_guide_orientation($i) == &Gimp::HORIZONTAL){
  70.             $keyval = sprintf("%4d", $img->get_guide_position($i));
  71.             $hkeys{$keyval} = $i;
  72.             }
  73.         }
  74.     # need to sort them in order of their occurance in the image
  75.     foreach $key(sort keys %hkeys) {
  76.         push @hguides, $hkeys{$key};
  77.         }
  78.     return @hguides;
  79.     }
  80.  
  81. #
  82. # Duplicate, crop and save the image fragment.
  83. #
  84.  
  85. sub dosel { 
  86.     ($img, $savepath, $imgpath, $imgbasename, $extension, $l,$r,$t,$b, $i,$j) = @_;
  87.     $filename =~ m/^(.*)\.[^\.]*$/ ;
  88.     $imgname = "$imgbasename-$i-$j.$extension";
  89.     $tmpimg = $img->channel_ops_duplicate;
  90.     # print "Cropping from $l to $r, $t to $b\n";
  91.     $tmpimg->crop($r-$l, $b-$t, $l, $t);
  92.     $tmpimg->Gimp::Fu::save_image("$savepath$imgpath$imgname","$savepath$imgpath$imgname");
  93.     $tmpimg->delete;
  94.     return "$imgpath$imgname"; # what I want printed in html
  95.     }
  96.  
  97. #
  98. # HTML Table Generation Functions
  99. #
  100.  
  101. sub html_table_start {
  102.     ($fn,$cellpadding,$cellspacing,$border,$capatalize) = @_;
  103.     $str = $capatalize ? 
  104. "<TABLE CELLSPACING=$cellspacing CELLPADDING=$cellpadding BORDER=$border>\n" :
  105. "<table cellspacing=$cellspacing cellpadding=$cellpadding border=$border>\n" ;
  106.     print $fn $str;
  107.     }
  108.  
  109. sub html_table_row_start {
  110.     ($fn, $capatalize) = @_;
  111.     $str = $capatalize ? "\t<TR>\n" : "\t<tr>\n";
  112.     print $fn $str;
  113.     }
  114.  
  115. sub html_table_entry {
  116.     ($fn, $imgname, $width, $height, $capatalize) = @_;
  117.     $str = $capatalize ? 
  118. "\t\t<TD><IMG ALT=\" \" SRC=\"$imgname\" WIDTH=\"$width\" HEIGHT=\"$height\"></TD>\n" :
  119. "\t\t<td><img alt=\" \" src=\"$imgname\" width=\"$width\" height=\"$height\"></td>\n";
  120.     print $fn $str;
  121.     }
  122.  
  123. sub html_table_row_end {
  124.     ($fn, $capatalize) = @_;
  125.     $str = $capatalize ? "\t</TR>\n" : "\t</tr>\n";
  126.     print $fn $str;
  127.     }
  128.  
  129. sub html_table_end {
  130.     ($fn, $capatalize) = @_;
  131.     $str = $capatalize ? "</TABLE>\n":"</table>\n";
  132.     print $fn $str;
  133.     }
  134.  
  135. # <tigert> Save-path: [_____________________][browse]
  136. # <tigert> html-file name: [_________________]
  137. # <tigert> image-basename [__________________]
  138. # <tigert> [x] use separate dir for images
  139. # <tigert> image directory: [___________________]
  140.  
  141. # later, decided to have UPPER/lower case HTML toggle
  142. # cellspacing: ___^
  143.  
  144. register "perlotine",
  145.  "Guilotine implemented ala perl, with html output",
  146.  "Add guides to an image.  Then run this.  It will cut along the guides, and give you the html to reassemble the resulting images.",
  147.  "Seth Burgess",
  148.  "Seth Burgess <sjburges\@gimp.org>",
  149.  "1999-03-19",
  150.  N_"<Image>/Filters/Web/Perl-o-tine...",
  151.  "*",
  152.  [
  153.   [PF_STRING, "save_path", "The path to export the HTML to",$ENV{HOME}],
  154.   [PF_STRING, "html_file_name", "Filename to export","perlotine.html"],
  155.   [PF_STRING, "image_basename", "What to call the images","perlotine"],
  156.   [PF_RADIO,  "image_extension", "The format of the images: {gif, jpg, png}", "gif", [gif => "gif", jpg => "jpg", png => "png"]],
  157.   [PF_TOGGLE, "separate_image_dir", "Use a separate directory for images?",0],
  158.   [PF_STRING, "relative_image_path", "The path to export the images to, relative to the Save Path", "images/"],
  159.   [PF_TOGGLE, "capitalize_tags", "Capatalize HTML tags?", 0],
  160.   [PF_SPINNER, "cellspacing", "Add space between the table elements", 0, [0,15,1]],
  161.   ], sub {
  162.  
  163.     my($img,$layer,$savepath, $htmlname, $imgbasename, $extension, $separate, $imgpath, $capatalize, $cellspacing) =@_;
  164.  
  165.     @vert = get_vguides($img);
  166.     @horz = get_hguides($img);
  167.  
  168.     if (!(scalar(@vert) || scalar(@horz))) {
  169.         die __"No horizontal or vertical guides found. Aborted.";
  170.         }
  171.  
  172.    # 
  173.    # Progress Bar
  174.    # 
  175.  
  176.    Gimp->progress_init("Perl-o-Tine");
  177.    $progress_increment = 1/( (scalar(@horz)+1) * (scalar(@vert)+1) );
  178.    $progress = 0.0;
  179.    
  180.    # (Debugging info for the guide functions)
  181.  
  182.    # print @vert, " LEN = ", scalar(@vert), "\n";
  183.    # print @horz, " LEN = ", scalar(@horz), "\n";
  184.    # foreach $guide (@vert) {
  185.    #        print $img->get_guide_position($guide), "\n";
  186.    #        }
  187.  
  188.   #
  189.   # Correctly format paths and filenames
  190.   # 
  191.   if (!($savepath=~ m,/$,)) {  # add a trailing slash if its not on there
  192.       $savepath = $savepath . "/";
  193.    }
  194.  
  195.    if (!($imgpath=~ m,/$,)) {  # add a trailing slash if its not on there
  196.     $imgpath= $imgpath . "/";
  197.    }
  198.    if (!$separate) { $imgpath = ""; }
  199.  
  200.     # create paths if they don't already exist
  201.     mkpath($savepath);
  202.     mkpath($savepath . $imgpath);
  203.  
  204.     # open HTML file for writing 
  205.     open FILE, ">$savepath$htmlname" or die "Couldn't open $savepath$htmlname: $!\n";
  206.  
  207.     $top=0;
  208.     html_table_start(\*FILE,0,$cellspacing,0,$capatalize);
  209.     for ($i=0; $i<=scalar(@horz); $i++) {
  210.         $bot = ($i>$#horz) ? $img->height : $img->get_guide_position($horz[$i]);
  211.         html_table_row_start(\*FILE, $capatalize);
  212.         $left=0;
  213.         for ($j=0; $j<=scalar(@vert); $j++) {
  214.             $right = ($j>$#vert) ? $img->width : $img->get_guide_position($vert[$j]);
  215.             $imgname = dosel($img, $savepath, $imgpath, $imgbasename, $extension, $left, $right, $top, $bot, $i, $j);
  216.             html_table_entry(\*FILE, $imgname, $right-$left, $bot-$top, $capatalize);
  217.             $left = $right + $cellspacing;
  218.  
  219.             # Increment the progress bar
  220.             $progress += $progress_increment;
  221.             Gimp->progress_update ($progress);
  222.             }
  223.         html_table_row_end(\*FILE, $capatalize);
  224.         $top = $bot + $cellspacing;
  225.         }
  226.     html_table_end(\*FILE, $capatalize);
  227.     return();
  228. };
  229. exit main;
  230.